;;   This file is part of scheme-GNUnet, a partial Scheme port of GNUnet.
;;   Copyright (C) 2006--2020 GNUnet e.V.
;;   Copyright (C) 2020 Maxime Devos <maxime.devos@student.kuleuven.be>
;;
;;   GNUnet is free software: you can redistribute it and/or modify it
;;   under the terms of the GNU Affero General Public License as published
;;   by the Free Software Foundation, either version 3 of the License,
;;   or (at your option) any later version.
;;
;;   GNUnet is distributed in the hope that it will be useful, but
;;   WITHOUT ANY WARRANTY; without even the implied warranty of
;;   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;;   Affero General Public License for more details.
;;
;;   You should have received a copy of the GNU Affero General Public License
;;   along with this program.  If not, see <http://www.gnu.org/licenses/>.
;;
;;   SPDX-License-Identifier: AGPL-3.0-or-later
;;
;;   As a special exception to the GNU Affero General Public License,
;;   the file may be relicensed under any license used for
;;   most source code of GNUnet 0.13.1, or later versions, as published by
;;   GNUnet e.V.

;; Extracted from src/include/gnunet_common.h

(library (gnu gnunet hashcode)
  (export hashcode-bit-length hashcode-u8-length
          short-hashcode-bit-length short-hashcode-u8-length
          hashcode? short-hashcode?
          bv->hashcode bv->short-hashcode
          hashcode->bv short-hashcode->bv)
  (import (rnrs base)
	  (rnrs control)
          (rnrs records syntactic)
	  (rnrs bytevectors))

  (define hashcode-bit-length 512)
  (define short-hashcode-bit-length 256)
  (define hashcode-u8-length (/ hashcode-bit-length 8))
  (define short-hashcode-u8-length (/ short-hashcode-bit-length 8))

  ;; A 512-bit hashcode.  These are the default length for GNUnet,
  ;; using SHA-512.
  (define-record-type (<hashcode> %make-hashcode hashcode?)
    (fields (immutable bv %hashcode-bv))
    (opaque #t)
    (sealed #t))

  ;; A 256-bit hashcode.  Used under special conditions, like when space
  ;; is critical and security is not impacted by it.
  (define-record-type (<short-hashcode> %make-short-hashcode short-hashcode?)
    (fields (immutable bv %short-hashcode-bv))
    (opaque #t)
    (sealed #t))

  (define (bv->something %wrap length)
    (case-lambda
      "Read something from a bytevector"
      ((bv) ; whole bytevector
       (assert (= (bytevector-length bv) length))
       (%wrap (bytevector-copy bv)))
      ((bv offset) ; part of a bytevector, starting at some offset
       (assert (<= (+ offset length) (bytevector-length bv)))
       (let ((bv-new (make-bytevector length)))
         (bytevector-copy! bv 0 bv-new 0 length)
         (%wrap bv-new)))))

  (define bv->hashcode
    (bv->something %make-hashcode hashcode-u8-length))
  (define bv->short-hashcode
    (bv->something %make-short-hashcode short-hashcode-u8-length))

  (define (hashcode->bv hashcode)
    "Extract the bytevector corresponding to @var{hashcode}
(read-only)"
    (bytevector-copy (%hashcode-bv hashcode)))

  (define (short-hashcode->bv hashcode)
    "Extract the bytevector corresponding to @var{short-hashcode}
(read-only)"
    (bytevector-copy (%short-hashcode-bv hashcode))))


